import javax.swing.*;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class UpgradeOrderFrame extends JFrame{
	 private String[] TechnologyName = {"Infantry", "Automatic Weapons", "Rocket Launchers", "Tanks", "Improved TAnks", "Fighter Planes"};
	 JLabel[] trooptype;
	 JComboBox number;
	 JComboBox<String> type;
	 JButton submit, cancel;
	 private Territory territory;
	 private int source;
	 MyHistoryPanel historyPanel;

	 public UpgradeOrderFrame(MyHistoryPanel _historyPanel, Territory _territory, int _source){
		super("Create a new upgrade order");
		territory = _territory;
		source = _source;
		historyPanel = _historyPanel;
		
		setLayout(new GridLayout(4, 2, 10, 10));	   
		
	  	add(new JLabel("Upgrade " + TechnologyName[source] + ": "));
	  	add(new JLabel("    "));
	  	
	  	number = new JComboBox();
	  	number.addItem(0);
	  	for (int i = 1; i <= territory.getTroopAmount(source); i++)
	  		number.addItem(i);
	  	type = new JComboBox<String>();
	  	for (int i = source + 1; i < 6; i++)
	  		type.addItem(TechnologyName[i]);
	  	add(new JLabel("To: "));
	  	add(type);
	  	add(new JLabel("Amount: "));
	  	add(number);
	  	
	  	submit = new JButton("Submit");
	  	cancel = new JButton("Cancel");
	  	
	  	add(submit);
	  	add(cancel);
	  	submit.addActionListener(new submitListener(this));
	  	cancel.addActionListener(new cancelListener(this));
	  	
	  	setSize(400, 200);
	  	setVisible(true);
	 }
	 
	class submitListener implements ActionListener{
   
		JFrame frame;
	  public submitListener(JFrame _frame){
			frame = _frame;
		}
	  public void actionPerformed(ActionEvent arg0) {
		int destination = type.getSelectedIndex() + source + 1;
		 int amount = (int)number.getSelectedItem();
	  	 UpgradeTroop temp = new UpgradeTroop(territory, source, destination, amount);
	     if (temp.checkOrder())
	  	    historyPanel.addHistory(temp);
	  	 frame.dispose();
	}
	 }
	 
	class cancelListener implements ActionListener{
		JFrame frame;
		public cancelListener(JFrame _frame){
			frame = _frame;
		}
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
		
	}
}

